/**
 * Classname :AccountAction.java
 *
 * Version information: 1.0
 *
 * Date: Dec 07, 2011 ho.viet.duan create
 *
 * MDG Viet Name co.ltd, All Rights Reserved.
 */

package com.zho.web.action.account;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zho.business.AccountManager;
import com.zho.common.domain.db.PagingInfo;
import com.zho.common.domain.db.SearchResult;
import com.zho.common.utility.Constants.Active;
import com.zho.domain.model.CustomerInfo;
import com.zho.util.Logit;
import com.zho.web.common.context.Utilities;
import com.zho.web.domain.model.AccountModel;

/**
 * 
 * @author Ho.Viet.Duan Created Dec 06, 2011
 */
public class AccountAction extends ActionSupport implements
		ModelDriven<AccountModel> {

	private static final long serialVersionUID = 1L;
	private Logit log = Logit.getInstance(AccountAction.class);
	AccountModel model = new AccountModel();

	AccountManager accountManager = null;

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public AccountModel getModel() {
		return model;
	}

	// Save customer
	public String addCustomer() throws Exception {
		try {
			log.info("addCustomer");
			// Save it
			if (model.getCustomerInfo() != null) {				
				if (validateAddingCustomer()) {
					model.getCustomerInfo().setActiveFlg(Active.ACTIVE);
					accountManager.addCustomer(model.getCustomerInfo());
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
			throw e;
		}
		model.setSuccessMessage(getText("message.customer.register.successfully"));
		
		return SUCCESS;

	}

	private boolean validateAddingCustomer() throws Exception {
		boolean result = true;
		if (model.getCustomerInfo() != null) {
			model.getCustomerInfo().setActiveFlg(Active.ACTIVE);
			
			// Check existing customer
			CustomerInfo cust = accountManager.getCustomerByEmail(model.getCustomerInfo().getEmailAddress());
			String strError = "";
			if (cust != null) {
				strError += getText("message.compare.email");
				model.setErrorMessage(strError);
				result = false;
			}

			// Check confirmation password
			if (model.getCustomerInfo().getPassword() != null
					&& model.getCustomerInfo().getPassword().equals(model.getCustomerInfo().getRePassword())) {
			} else {
				strError += getText("message.compare.password");
				model.setErrorMessage(strError);
				result = false;
			}
			
			// Age is greater than 18
			if (model.getCustomerInfo().getDob() != null) {
				Date current = new Date();
				Date dob = (Date)model.getCustomerInfo().getDob().clone();
				dob.setYear(dob.getYear() + 18);
				if (current.before(dob)) {
					strError += getText("message.invalid.dateofbirth");
					model.setErrorMessage(strError);
					result = false;
				}				
			}
		}
		return result;
	}
	
	/**
	 * List UnFriends
	 */
	public String listCustomer() throws Exception {
		
		try {
			
			CustomerInfo customerInfo = model.getCustomerInfo();
			PagingInfo pagingInfo = model.getPagingInfo();			
			String orderColumn = model.getOrderColumn();
			
			if (customerInfo == null) customerInfo = new CustomerInfo();
			
			customerInfo.setFriendId(Utilities.getCurrentLoginUserId());
			customerInfo.setActiveFlg(Active.ACTIVE);
			
			if (pagingInfo == null) pagingInfo = new PagingInfo();
			SearchResult<CustomerInfo> customerList = accountManager.listCustomer(customerInfo, pagingInfo, orderColumn);
			
			// Result
			pagingInfo = customerList.getPagingInfo();
			model.setPagingInfo(pagingInfo);
			model.setCustomerInfoList(customerList);			
			
			
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
			throw e;
		}
		return SUCCESS;
	}

	/**
	 * Edit Profile
	 */
	public String editProfile() throws Exception {
		try {
			// Get current login
			String id = Utilities.getCurrentLoginUserId();
			if (!StringUtils.isEmpty(id)) {
				CustomerInfo customerInfo = accountManager.getCustomerById(id);
				model.setCustomerInfo(customerInfo);			
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
			throw e;
		}
		return SUCCESS;		
	}
	
	/**
	 * Get user profile 
	 */
	public String getProfile() throws Exception {
		try {
			if (!StringUtils.isEmpty(Utilities.getCurrentLoginUserId())) {
				String customerId = "";
				if (model.getCustomerInfo() != null && !StringUtils.isEmpty(model.getCustomerInfo().getCustomerId())) {
					customerId = model.getCustomerInfo().getCustomerId();
				} else {
					customerId = Utilities.getCurrentLoginUserId();
				}
				CustomerInfo customerInfo = accountManager.getCustomerById(customerId);
				if (customerInfo != null) {
					model.setCustomerInfo(customerInfo);
				}
			}			
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(), e);
			throw e;
		}
		return SUCCESS;
	}
}